package org.zhouhe.modules.article.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.zhouhe.base.BaseAction;
import org.zhouhe.modules.article.service.ArticleService;
import org.zhouhe.modules.report.service.ReportService;
import org.zhouhe.page.PageUtil;
import org.zhouhe.util.InitSystem;
import org.zhouhe.vo.Article;
import org.zhouhe.vo.ArticleArticleType;
import org.zhouhe.vo.ArticleIndustry;
import org.zhouhe.vo.ArticleReco;
import org.zhouhe.vo.ArticleReport;
import org.zhouhe.vo.ArticleType;
import org.zhouhe.vo.Industry;
import org.zhouhe.vo.Report;

public class ArticleAction extends BaseAction {

	private static final long serialVersionUID = 896164905733014622L;
	private PageUtil pageUtil = new PageUtil();
	private Article article;
	private String displayRecoDiv = "none";
	private ArticleType paramArticleType = new ArticleType();
	private Industry paramIndustry = new Industry();
	private List<Industry> industryList = new ArrayList<Industry>();
	private List<ArticleIndustry> articleIndustries = new ArrayList<ArticleIndustry>();
	private List<ArticleArticleType> articleArticleTypes = new ArrayList<ArticleArticleType>();
	private List<ArticleReco> articleRecoList = new ArrayList<ArticleReco>();
	private List<Report> reportList = new ArrayList<Report>();
	private List<ArticleReport> articleReportList = new ArrayList<ArticleReport>();
	private String keywords;
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ReportService reportService;
	public String reportListByKeywords(){
		Report report = new Report();
		report.setTitle(keywords);
		reportService.getReportList(pageUtil, report, null, null);
		return "reportListByKeywords";
	}
	public String saveOrUpdate() {
		if (article != null && article.getArticleId() != null) {
			article = articleService.getArticleDetail(article);
		} else {
			article = new Article();
		}
		if (article.getQuoteFrom() == null) {
			article.setQuoteFrom(InitSystem.paramMap.get("websiteName"));
		}
		if(article.getDisplayTime() == null){
			article.setDisplayTime(new Timestamp(new Date().getTime()));
		}
		if (this.pageUtil == null) {
			this.pageUtil = new PageUtil();
		}
		for(ArticleArticleType aat : article.getArticleArticleTypes()){
			ArticleType at = aat.getArticleType();
			if(new Integer(1).equals(at.getArticleType().getArticleTypeId())){
				this.displayRecoDiv = "block";
			}
		}
		for(ArticleIndustry ai:article.getArticleIndustries()){
			Industry pIndustry = ai.getIndustry().getIndustry();
			if(!industryList.contains(pIndustry)){
				pIndustry.setIndustries(new LinkedHashSet<Industry>());
				this.resetFlag4reco(article.getArticleRecos(), pIndustry);
				industryList.add(pIndustry);
			}
			Industry indu = ai.getIndustry();
			this.resetFlag4reco(article.getArticleRecos(), indu);
			pIndustry.getIndustries().add(indu);
			
		}
		return "saveOrUpdate";
	}
	private void resetFlag4reco(Set<ArticleReco> articleRecos, Industry indu){
		indu.setStatus(0);//not checked
		indu.setOrderId(0);//not checked
		for(ArticleReco reco:articleRecos){
			if(reco.getIndustry().getIndustryId().equals(indu.getIndustryId())){
				indu.setStatus(1);//checked
				if(new Integer(1).equals(reco.getFirstPosition())){
					indu.setOrderId(1);//checked
				}
				break;
			}
		}
	}

	public String saveOrUpdateDo() {
		 HttpServletRequest req = ServletActionContext.getRequest();
		 for(int i=0; i<3; i++){
			 System.out.println(">>>"+req.getParameter("articleIndustries["+i+"].industry.industryId"));
		 }
		article.setArticleIndustries(new LinkedHashSet<ArticleIndustry>(articleIndustries));
		article.setArticleArticleTypes(new LinkedHashSet<ArticleArticleType>(articleArticleTypes));
		article.setArticleRecos(new LinkedHashSet<ArticleReco>(articleRecoList));
		article.setArticleReports(new LinkedHashSet<ArticleReport>(articleReportList));
		String keywords = article.getKeywords();
		if(keywords != null){
			keywords = keywords.replaceAll("[\r]+", "");//换行 => 
			keywords = keywords.replaceAll("[\n]+", " ");//换行 => 半角空格
			keywords = keywords.replaceAll("[　]+", " ");//全角空格 => 半角空格
			keywords = keywords.replaceAll("[ ]+", ",");//多个连续空格 => 一个英文逗号（分隔符）
			keywords = keywords.replaceAll("[,,]+", ",");
			article.setKeywords(keywords);
		}
		if (article.getArticleId() == null) {// save
			articleService.save(article);
		} else {// update
			articleService.update(article);
		}
		return "saveOrUpdateDo";
	}

	public String delete() {
		articleService.delete(article);
		return "delete";
	}

	public String list() {
		if (getPageUtil() == null)
			setPageUtil(new PageUtil());
		articleService.getArticleList(getPageUtil(), article, paramIndustry.getIndustryId(), paramArticleType.getArticleTypeId());
		return "list";
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticleIndustries(List<ArticleIndustry> articleIndustries) {
		this.articleIndustries = articleIndustries;
	}

	public List<ArticleIndustry> getArticleIndustries() {
		return articleIndustries;
	}

	public void setArticleArticleTypes(List<ArticleArticleType> articleArticleTypes) {
		this.articleArticleTypes = articleArticleTypes;
	}

	public List<ArticleArticleType> getArticleArticleTypes() {
		return articleArticleTypes;
	}

	public void setParamArticleType(ArticleType paramArticleType) {
		this.paramArticleType = paramArticleType;
	}

	public ArticleType getParamArticleType() {
		return paramArticleType;
	}

	public void setParamIndustry(Industry paramIndustry) {
		this.paramIndustry = paramIndustry;
	}

	public Industry getParamIndustry() {
		return paramIndustry;
	}

	public void setIndustryList(List<Industry> industryList) {
		this.industryList = industryList;
	}

	public List<Industry> getIndustryList() {
		return industryList;
	}
	public void setArticleRecoList(List<ArticleReco> articleRecoList) {
		this.articleRecoList = articleRecoList;
	}
	public List<ArticleReco> getArticleRecoList() {
		return articleRecoList;
	}
	public void setDisplayRecoDiv(String displayRecoDiv) {
		this.displayRecoDiv = displayRecoDiv;
	}
	public String getDisplayRecoDiv() {
		return displayRecoDiv;
	}
	public void setReportList(List<Report> reportList) {
		this.reportList = reportList;
	}
	public List<Report> getReportList() {
		return reportList;
	}
	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}
	public String getKeywords() {
		return keywords;
	}
	public void setArticleReportList(List<ArticleReport> articleReportList) {
		this.articleReportList = articleReportList;
	}
	public List<ArticleReport> getArticleReportList() {
		return articleReportList;
	}
}
